package org.dev.lms.controller;

import org.apache.commons.lang3.StringUtils;
import org.dev.lms.entity.BookInventory;
import org.dev.lms.entity.LibraryBranch;
import org.dev.lms.service.BookInventoryService;
import org.dev.lms.service.BookService;
import org.dev.lms.entity.Book;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.dev.common.core.page.PaginAtion;
import org.dev.common.core.result.ResponseResult;
import org.dev.lms.service.LibraryBranchService;
import org.dev.lms.util.FlowCodeCons;
import org.dev.util.util.SequenceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.dev.common.core.aop.OperLog;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    public BookService bookService;
    @Autowired
    SequenceUtil sequenceUtil;
    @Autowired
    public BookInventoryService bookInventoryService;
    @Autowired
    public LibraryBranchService libraryBranchService;

    /**
     * 保存和修改公用的
     *
     * @param book 传递的实体
     * @return ResponseResult转换结果
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @OperLog(description = "book保存和修改")
    public ResponseResult<String> save(@RequestBody Book book) {
        boolean result = false;
        try {
            if (book.getId() != null) {
                result = bookService.updateById(book);
            } else {
                book.setBookBarCode(sequenceUtil.GeneratorCode(FlowCodeCons.BOOK_NUM, book.getCatalogCode()));
                result = bookService.save(book);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        if (result)
            return ResponseResult.success();
        return ResponseResult.error();
    }

    /**
     * 获取对象信息
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    @OperLog(description = "book获取对象信息")
    public ResponseResult<Book> get(@RequestParam("id") Long id) {
        Book book = bookService.getById(id);
        //获取图书的库存
        BookInventory bookInventory = new BookInventory();
        bookInventory.setBookId(id);
        List<BookInventory> bookInventoryList = this.bookInventoryService.list(new QueryWrapper<>(bookInventory));
        book.setInventoryList(bookInventoryList);
        //
        if (bookInventoryList != null && bookInventoryList.size() > 0) {
            List<Long> longList = bookInventoryList.stream().map(BookInventory::getLibraryId).distinct().collect(Collectors.toList());
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.in("id", longList);
            List<LibraryBranch> libraryBranchList = this.libraryBranchService.list(queryWrapper);
            if (libraryBranchList != null && libraryBranchList.size() > 0) {
                libraryBranchList.forEach(libraryBranch -> {
                    bookInventoryList.forEach(bookInventory1 -> {
                        if (libraryBranch.getId().equals(bookInventory1.getLibraryId())) {
                            bookInventory1.setLibraryName(libraryBranch.getLibraryName());
                        }
                    });
                });
            }
        }
        return ResponseResult.success(book);
    }

    /**
     * 根据图书编码加载图书信息
     * @param bookBarCode
     * @return
     */
    @GetMapping("/get-by-code")
    @OperLog(description = "book获取对象信息")
    public ResponseResult<Book> getByCode(@RequestParam("bookBarCode") String bookBarCode) {
        Book book = new Book();
        book.setBookBarCode(bookBarCode);
        return ResponseResult.success(this.bookService.getOne(new QueryWrapper<>(book)));
    }


    /**
     * 删除对象信息
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @OperLog(description = "book删除对象信息")
    public ResponseResult<String> delete(@RequestBody List<Long> ids) {
        try {
            bookService.removeByIds(ids);
            return ResponseResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("删除对象失败！" + e.getMessage());
        }
    }

    /**
     * 分页查询数据信息
     *
     * @param book
     * @param pagination
     * @return
     */
    @RequestMapping("/list")
    @OperLog(description = "book分页查询数据信息")
    public ResponseResult<IPage<Book>> list(Book book, PaginAtion pagination) {
        Page page = pagination.getPage();
        QueryWrapper<Book> queryWrapper = new QueryWrapper<Book>(book);
        if (!StringUtils.isEmpty(book.getKeyWords())) {
            queryWrapper.and(wrapper -> wrapper.like("book_name", book.getKeyWords()).or().like("publisher", book.getKeyWords()).or().like("author", book.getKeyWords()));
        }
        return ResponseResult.success(bookService.page(page, queryWrapper));
    }
}